﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home_ListBookActive : System.Web.UI.Page
{
    public LanguageManage Language;
    public BookAdapter bookAdapter;
    UserEntity currentUser;
    public long catelogyID;
    public long platformID;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check login
        currentUser = UserManager.Instance.Getuser();
        if (!currentUser.isLogin)
            Response.Redirect("~/Login.aspx");
        Language = new LanguageManage();
        if (!IsPostBack)
        {
            int year = DateTime.Now.Year;
            for (int i = year - 10; i <= year; i++)
            {
                comboYear.Items.Add(i.ToString());
            }
            for (int i = 1; i <= 12; i++)
            {
                comboMonth.Items.Add(i.ToString());
            }
            if (Session["day"] != null)
            {
                comboMonth.SelectedValue = Session["month"].ToString();
                comboYear.SelectedValue = Session["year"].ToString();
                comboDay.Items.Clear();
                int y = int.Parse(comboYear.SelectedValue);
                int m = int.Parse(comboMonth.SelectedValue);
                DateTime date = new DateTime(y, m, 1);
                int d = 1;
                while (date.Year == y && date.Month == m)
                {
                    comboDay.Items.Add(d.ToString());
                    d++;
                    date = date.AddDays(1);
                }
                comboDay.SelectedValue = Session["day"].ToString();
            }
            else
            {
                for (int i = 1; i <= 31; i++)
                {
                    comboDay.Items.Add(i.ToString());
                }
            }
            
        }
        if (Session["listBookActive"] != null)
        {
            // catelogyID = Convert.ToInt32(Request.QueryString["catelogyid"]);
            Session["menu_current"] = Session["menu_id"];
            Session["catelogyID"] = catelogyID;
            bookAdapter = new BookAdapter();
            List<BookEntity> list = bookAdapter.getListBook(1, currentUser);
            ListBookActive1.listBook = (List<BookEntity>)Session["listBookActive"];
        }
        else
        {
            ListBookActive1.listBook = new List<BookEntity>();
        }  
    }
    protected void comboMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboDay.Items.Clear();
        int year=int.Parse(comboYear.SelectedValue);
        int month=int.Parse(comboMonth.SelectedValue);
        DateTime date=new DateTime(year,month,1);
        int d=1;
        while (date.Year == year &&date.Month==month)
        {
            comboDay.Items.Add(d.ToString());            
            d++;
            date =date.AddDays(1);
        }
    }
    protected void btnActive_Click(object sender, EventArgs e)
    {
        List<BookEntity> list=(List<BookEntity>)Session["listBookActive"];
        for (int i = 0; i < list.Count; i++)
        {
            string str = Request.Form["checkItem_"+list[i].ID];
            if (str != null)
            {
                bookAdapter = new BookAdapter();
                bookAdapter.ActiveBook(list[i].ID,true);
            }
        }
        string day = comboDay.SelectedValue;
        string year = comboYear.SelectedValue;
        string month = comboMonth.SelectedValue;
        Session["listBookActive"] = bookAdapter.getBookByDate(currentUser, day, month, year);
        Response.Redirect("~/Home/ListBookActive.aspx");        
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        string day = comboDay.SelectedValue;
        string year = comboYear.SelectedValue;
        string month = comboMonth.SelectedValue;
        bookAdapter = new BookAdapter();
        Session["day"] = day;
        Session["month"] = month;
        Session["year"] = year;
        Session["listBookActive"] = bookAdapter.getBookByDate(currentUser, day, month, year);
        Response.Redirect("~/Home/ListBookActive.aspx");
    }
}
